var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        page: 1,
        listRows: 20,
        total: 0,
        classId:''
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
    },
    getData: function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            page: _this.params.page,
            listRows: _this.params.listRows,

        };
        console.log(paramsObj);
        Api.adminquestionlist(paramsObj, function(data){
            console.log(data);
            _this.getDataList(data.arrList);
            _this.params.total = data.totalPages;
            layui.use('laypage', function() {
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'page',
                    index: 1,
                    theme: '#3498db',
                    curr: _this.params.page,
                    count: _this.params.total, //数据总数，从服务端得到
                    hash: 'curr_page',
                    layout: ['prev', 'page', 'next', 'skip'],
                    jump: function(obj, first) {
                        _this.params.page = obj.curr;
                        //obj包含了当前分页的所有参数，比如：curr当前页
                        var Params = {
                            passport: _this.params.passport,
                            page: _this.params.page,
                            listRows: _this.params.listRows,

                        };
                        Api.adminquestionlist(Params, function(data) {
                            _this.getDataList(data.arrList);

                        })
                    }
                });
            });
        });
    },
    getDataList: function(data) {
        var _this = this;
        if (data && data.length > 0) {
            var html = '',
                i = 0,
                len = data.length;
            for (i; i < len; i++) {
                html += '<tr>' +
                        // '<td class="text-c" width="24">'+
                        //     '<div class="goods-checkbox">'+
                        //         '<div class="check-box" questionId="' + data[i].questionId + '">'+
                        //             '<input type="checkbox" name="new" questionId="' + data[i].questionId + '">'+
                        //         '</div>'+
                        //     '</div>'+
                        // '</td>'+
                    '<td class="text-c">' + data[i].questionId + '</td>' +
                    // '<td class="text-c"><img src="'+data[i].imgUrl+'" style="widht:100px;height:100px;"></td>'+
                    '<td class="text-c">' + data[i].title + '</td>'+
                    '<td class="text-c">' + data[i].className + '</td>';
                if (data[i].isShow==1) {
                    html+='<td class="text-c"><span questionId="'+data[i].questionId+'" class="switch switch-on new" themeColor="#3498db"></span></td>';
                }else{
                    html+='<td class="text-c"><span questionId="'+data[i].questionId+'" class="switch switch-off new" themeColor="#3498db"></span></td>';
                };
                    html+='<td class="text-c">' + data[i].gmtCreate + '</td>' +
                         '<td class="text-c">' +
                        '<p><a questionId="' + data[i].questionId + '"  class="handle view">查看</a><a moduleName="' + data[i].moduleName + '" questionId="' + data[i].questionId + '"  class="handle edit">编辑</a><a questionId="' + data[i].questionId + '"  class="handle delete">删除</a></p>' +
                        '</td>' +
                    '</tr>';
            }
            $('#List').html(html);
            $('tfoot').css('display', 'table-footer-group');
            _this.getDataEvent();
        } else {
            var html = '<tr>' +
                '<td class="text-c c-666" colspan="6" >暂无数据</td>' +
                '</tr>';
            $('#List').html(html);
            $('tfoot').css('display', 'none');
        }
    },
    getDataEvent: function() {
        var _this = this;
        honeySwitch.init();
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('.switch').on('click',function(event) {
            event.preventDefault();
            var questionId=$(this).attr('questionId');
            var datas=null;
            if ($(this).hasClass('switch-on')) {
                datas=1;
            }else{
                datas=0;
            };
            _this.changeShow(questionId,datas);
        });

        $('.edit').on('click', function() {
            Pub.setSession('questionId', $(this).attr('questionId'));
            Pub.setSession('moduleName', $(this).attr('moduleName'));
            window.location.href = Pub.getHtmlUrl('./help-list-edit.html');
        });
        $('.view').click(function(event) {
            Pub.setSession('questionId', $(this).attr('questionId'));
            layui.use('layer', function(){
                var index = layer.open({
                    type: 2,
                    title: '帮助详情',
                    content: './help-list-detail.html'
                });
                layer.full(index);
            });
        });
        $('.delete').on('click', function(event) {
            event.preventDefault();
            var questionId=$(this).attr('questionId');
            layui.use('layer', function(){
                layer.confirm('您确定要删除吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(index){
                    _this.deleteEvent(questionId);
                    layer.close(index);
                }, function(){

                });
            });
        });

    },
    changeShow:function(questionId,isShow){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            questionId:parseInt(questionId),
            isShow:isShow,
        };
        console.log(paramsObj);
        Api.adminquestionshowupdate(paramsObj, function(data){
            console.log(data);
        });
    },
    deleteEvent:function(questionId){
        var _this = this;
        var Params = {
            passport: _this.params.passport,
            questionId:questionId
        };
        console.log(Params);
        Api.adminquestiondelete(Params, function(data) {
            _this.params.page=1;
            _this.getData();

        })
    },
    getClassify:function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            page:1,
            listRows:1000
        };
        Api.adminquestionclasslist(paramsObj, function(data){
            console.log(data);
            _this.getClassifyList(data.arrList)
        });
    },
    getClassifyList:function(data){
        var _this = this;
        if (data && data.length > 0) {
            var html = '<option value="" selected>全部</option>', i = 0, len = data.length;
            for (i; i < len; i++) {
                html+='<option value="'+data[i].classId+'">'+data[i].name+'</option>';
            }
            $('#typeSelect').html(html);
            _this.getClassifyEvent();

        }
    },
    getClassifyEvent:function(){
        var _this=this;
        $('#typeSelect').on('change', function(event) {
            event.preventDefault();
            _this.params.classId=$(this).val();
        });
    },
    otherEvent:function(){
        var _this = this;
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('.slectShow').click(function(event) {
            $('.select-wrap').toggle(400);
        });

    },
    initEvent: function(){
        var _this = this;
         _this.getData();
        _this.otherEvent();
        _this.getClassify();

    }
};
var page = new Page();